package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.vo.ImageBizDetailVO;
import com.fdb.efp.nls.service.dao.ImageBizDetailDao;
import com.fdb.efp.nls.service.domain.ImageBizDetail;
import com.fdb.efp.nls.service.facade.ImageBizDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 影像业务文件
 * <p>
 * 
 * @author ligm
 * @date 2019-01-03
 */
@Service("imageBizDetailService")
public class ImageBizDetailServiceImpl implements ImageBizDetailService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(ImageBizDetailServiceImpl.class);

	@Autowired
	private ImageBizDetailDao imageBizDetailDao;

	/**
	 * 新增操作
	 */
	public int insertImageBizDetail(ImageBizDetailVO inImageBizDetailVo) {
		logger.debug("当前新增数据为:" + inImageBizDetailVo.toString());
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.insertImageBizDetail(imageBizDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(ImageBizDetailVO inImageBizDetailVo) {
		logger.debug("当前删除数据条件为:" + inImageBizDetailVo);
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.deleteByPk(imageBizDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inImageBizDetailVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(ImageBizDetailVO inImageBizDetailVo) {
		logger.debug("当前修改数据为:" + inImageBizDetailVo.toString());
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.updateByPk(imageBizDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inImageBizDetailVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public ImageBizDetailVO queryByPk(ImageBizDetailVO inImageBizDetailVo) {

		logger.debug("当前查询参数信息为:" + inImageBizDetailVo);
		try {
			ImageBizDetail queryImageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, queryImageBizDetail);
			ImageBizDetail queryRslImageBizDetail = imageBizDetailDao.queryByPk(queryImageBizDetail);
			if (Objects.nonNull(queryRslImageBizDetail)) {
				ImageBizDetailVO outImageBizDetailVo = beanCopy(queryRslImageBizDetail, new ImageBizDetailVO());
				logger.debug("当前查询结果为:" + outImageBizDetailVo.toString());
				return outImageBizDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<ImageBizDetailVO> queryAllOwner(ImageBizDetailVO imageBizDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<ImageBizDetailVO> list = null;
		try {
			List<ImageBizDetail> imageBizDetails = imageBizDetailDao.queryAllOwnerByPage(imageBizDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + imageBizDetails.size());
			pageSet(imageBizDetails, imageBizDetailVo);
			list = (List<ImageBizDetailVO>) beansCopy(imageBizDetails, ImageBizDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<ImageBizDetailVO> queryAllCurrOrg(ImageBizDetailVO imageBizDetailVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<ImageBizDetail> imageBizDetails = imageBizDetailDao.queryAllCurrOrgByPage(imageBizDetailVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + imageBizDetails.size());
		List<ImageBizDetailVO> list = null;
		try {
			pageSet(imageBizDetails, imageBizDetailVo);
			list = (List<ImageBizDetailVO>) beansCopy(imageBizDetails, ImageBizDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<ImageBizDetailVO> queryAllCurrDownOrg(ImageBizDetailVO imageBizDetailVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<ImageBizDetail> imageBizDetails = imageBizDetailDao.queryAllCurrDownOrgByPage(imageBizDetailVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + imageBizDetails.size());
		List<ImageBizDetailVO> list = null;
		try {
			pageSet(imageBizDetails, imageBizDetailVo);
			list = (List<ImageBizDetailVO>) beansCopy(imageBizDetails, ImageBizDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public int deleteByApplySeq(ImageBizDetailVO imageBizDetailVo) {
		logger.debug("当前删除数据条件为:" + imageBizDetailVo);
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(imageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.deleteByApplySeq(imageBizDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + imageBizDetailVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 根据流水号查询数据
	 */
	@Override
	public List<ImageBizDetailVO> queryByApplySeqAndImageType(ImageBizDetailVO imageBizDetailVo) {
		logger.debug("根据流水号查询数据信息的参数信息为:");
		List<ImageBizDetail> imageBizDetails = imageBizDetailDao.queryByApplySeqAndImageType(imageBizDetailVo);
		logger.debug("根据流水号查询数据信息的结果集数量为:" + imageBizDetails.size());
		List<ImageBizDetailVO> list = null;
		try {
			pageSet(imageBizDetails, imageBizDetailVo);
			list = (List<ImageBizDetailVO>) beansCopy(imageBizDetails, ImageBizDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public int batchInsertImageBizDetail(List<ImageBizDetailVO> list) {
		logger.debug("当前新增数据为:" + JSONObject.toJSONString(list));
		int num = 0;
		try {
			num = imageBizDetailDao.batchInsertImageBizDetail(list);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}
    /**
     * 增加或者更新影像业务明细文件: <br>
     *
     * 0.1:caoxin:2019/4/4 8:37: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/4/4 8:37
     * @date 2019/4/4 8:37
     * @version 0.1
     */

	@Override
	public int insertOrUpdateImageBizDetail(ImageBizDetailVO imageBizDetailVo) {
		logger.debug("当前新增或更新的数据为:" + imageBizDetailVo.toString());
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(imageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.insertOrUpdateImageBizDetail(imageBizDetailVo);
		} catch (Exception e) {
			logger.error("当前新增或更新发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增或更新的数据为:" + num);
		return num;
	}

	@Override
	public int updateByApplySeqAndImageType(ImageBizDetailVO inImageBizDetailVo) {
		logger.debug("当前修改数据为:" + inImageBizDetailVo.toString());
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.updateByApplySeqAndImageType(imageBizDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inImageBizDetailVo + "修改的数据条数为" + num);
		return num;
	}

	@Override
	public int updateByCondition(ImageBizDetailVO inImageBizDetailVo) {
		logger.debug("当前修改数据为:" + inImageBizDetailVo.toString());
		int num = 0;
		try {
			ImageBizDetail imageBizDetail = new ImageBizDetail();
			beanCopy(inImageBizDetailVo, imageBizDetail);
			num = imageBizDetailDao.updateByCondition(imageBizDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inImageBizDetailVo + "修改的数据条数为" + num);
		return num;
	}

	@Override
	public List<ImageBizDetailVO> queryByCondition(ImageBizDetailVO imageBizDetailVo) {
		logger.debug("根据流水号查询数据信息的参数信息为:");
		List<ImageBizDetail> imageBizDetails = imageBizDetailDao.queryByCondition(imageBizDetailVo);
		logger.debug("根据流水号查询数据信息的结果集数量为:" + imageBizDetails.size());
		List<ImageBizDetailVO> list = null;
		try {
			pageSet(imageBizDetails, imageBizDetailVo);
			list = (List<ImageBizDetailVO>) beansCopy(imageBizDetails, ImageBizDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	
	/**
	 * 
	 * 存量影像信息到影像文件信息有数据则更新，无则新增 <br>
	 * 0.1:weijj:2019年5月7日 下午2:37:18:新增 <br>
	 * 
	 * @author weijj
	 * @since 2019年6月4日 下午2:37:18
	 * @date 2019年6月4日 下午2:37:18
	 * @version 0.1
	 */
@SuppressWarnings("unchecked")
@Override
public int insertOrUpdateImageBizDetailList(List<ImageBizDetailVO> list) {
	// TODO Auto-generated method stub
	LocalDate today = LocalDate.now();
	String msg1 = "【影像业务文件表】数据同步开始..." + today;
	logger.info(msg1, "message{}");
	List<ImageBizDetail> imageBizDetailList = new ArrayList<ImageBizDetail>();
	int num = 0;
	try {
		imageBizDetailList = (List<ImageBizDetail>) beansCopy(list, ImageBizDetail.class);
		num = imageBizDetailDao.insertOrUpdateImageBizDetailList(imageBizDetailList);
		String num_msg = "【影像业务文件表】插入或更新数据处理结果:" + num;
		logger.info(num_msg, "message{}");
	} catch (Exception e) {
		String msg2 = "【影像业务文件表】插入或更新数据出现异常:" + e;
		logger.error(msg2, "message{}");
		num = -1;
	}
	return num;
}
}
